package base.day08;

import java.util.HashSet;

/**
 * 描述：
 * 打印字符串的所有排列组合
 *
 * @author hl
 * @version 1.0
 * @date 2020/11/2 9:35
 */
public class Print_All_Permutations {
    public static void printAllPermutations(String str) {
        if (str == null) {
            return;
        }
        process(str.toCharArray(), 0);
    }

    private static void process(char[] chs, int i) {
        if (i == chs.length) {
            System.out.println(String.valueOf(chs));
        }
        HashSet<Character> set = new HashSet<>();
        for (int j = i; j < chs.length; j++) {
            if (set.contains(chs[j])) continue;
            set.add(chs[j]);
            swap(chs, i, j);
            process(chs, i + 1);
            swap(chs, i, j);
        }
    }

    private static void swap(char[] arr, int i, int j) {
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    public static void main(String[] args) {
        String string = "abc";
        printAllPermutations(string);
    }
}
